package wnhz.com.business.bindphone;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.uitls.ConfirmUtils;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.StatusBarUtil;
import wnhz.com.business.uitls.citychose.CityCodeChoseActivity;


public class BoundPhoneActivity extends BaseActivity{

    @BindView(R.id.v_statusbar)
    View vStatusbar;
    @BindView(R.id.tv_countryname1)//国家名字
    TextView tvCountryname;
    @BindView(R.id.tv_bindphone1_countrytel)//国家区号
    TextView tvBindphoneCountrytel;
    @BindView(R.id.ed_bindphone1_tel)//输入框_电话号码
    EditText edBindphoneTel;
    @BindView(R.id.btn_bindphone1_next)//按钮_完成
    Button btnBindphone1Next;

    private String code = "";
    private String countryname = "";
    private int RESULT_GETCITYCODE = 200;
    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, BoundPhoneActivity.class);
        return intent;
    }

    public void parseIntent() {
        Intent intent = getIntent();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bound_phone);
        ButterKnife.bind(this);
        parseIntent();
        initView();

        //状态栏设置
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.transparent), 0);
        StatusBarUtil.setDarkMode(this, true);
        StatusBarUtil.immersive(this, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            //透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
////            //透明导航栏
////            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
////            //改变状态栏颜色
////            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            vStatusbar.setVisibility(View.VISIBLE);
            //获取到状态栏的高度
            int statusHeight = getStatusBarHeight();
            //动态的设置隐藏布局的高度
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) vStatusbar.getLayoutParams();
            params.height = statusHeight;
            vStatusbar.setLayoutParams(params);
        }

    }

    private void initView() {
        if (!"".equals(Prefer.getInstance().getPhone())){ //获取手机号
            edBindphoneTel.setText(Prefer.getInstance().getPhone());
        }
        //根据输入框内容来设置登录按钮的背景
        edBindphoneTel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(edBindphoneTel.getText().toString())) {
                    if (count == 0) {
                        btnBindphone1Next.setBackground(getResources().getDrawable(R.drawable.bg_circle_grayd));
                        btnBindphone1Next.setEnabled(false);
                        btnBindphone1Next.setClickable(false);
                    } else {
                        btnBindphone1Next.setBackground(getResources().getDrawable(R.drawable.bg_circle_reds));
                        btnBindphone1Next.setEnabled(true);
                        btnBindphone1Next.setClickable(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @OnClick({R.id.rl_register_back,R.id.rl_chose_country1,R.id.btn_bindphone1_next})
    void Click(View v){
        Intent intent = null;
        switch (v.getId()){
            case R.id.rl_register_back:
                finish();
                break;
            case R.id.rl_chose_country1://选择国家
                intent = new Intent(BoundPhoneActivity.this, CityCodeChoseActivity.class);
                startActivityForResult(intent, RESULT_GETCITYCODE);
                break;
            case R.id.btn_bindphone1_next://下一步
                if (TextUtils.isEmpty(edBindphoneTel.getText().toString())){
                    MyToast("请输入手机号码");
                    return;
                }
                if (!ConfirmUtils.isMobileNO(edBindphoneTel.getText().toString())){
                    MyToast("手机号码格式不正确");
                    return;
                }
                intent = new Intent(BoundPhoneActivity.this, BindPhone2Activtity.class);
                intent.putExtra("phone", edBindphoneTel.getText().toString());
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_GETCITYCODE && resultCode == 0) {
            if (data != null) {
                /**
                 * 获取国家地区的电话区号和国家名称
                 */
                code = data.getStringExtra("code");
                countryname = data.getStringExtra("area");
                tvCountryname.setText(countryname);
                tvBindphoneCountrytel.setText(code);
            }
        }
    }
}
